/*
 * @Author: xiaosihan 
 * @Date: 2025-01-27 12:09:52 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2025-01-27 12:46:54
 */

import { autorun } from "mobx";
import { ReactNode, useEffect, useRef, useState } from "react";
import styles from "./runTime.module.less";
import homeStore from "@views/home/homeStore";
import { Button } from "antd";
import homeRenderer from "@views/home/homeRender";

interface Iprops {
    children?: ReactNode
}

// 运行时间
export default function RunTime() {

    const dom = useRef<HTMLDivElement | null>(null);
    const [runtime, setRuntime] = useState(homeStore.runtime);

    useEffect(() => {

    }, []);

    useEffect(() => {
        let startTime = 0;
        function beforeRender(){
            startTime = new Date().valueOf();
        }
        function afterRender(){
            homeStore.setRuntime(new Date().valueOf() - startTime);
        }
        homeRenderer.addEventListener("beforeRender", beforeRender);
        homeRenderer.addEventListener("afterRender", afterRender);
        return () => {
            homeRenderer.removeEventListener("beforeRender", beforeRender);
            homeRenderer.removeEventListener("afterRender", afterRender);
        }
    }, []);

    useEffect(() => autorun(() => {
        setRuntime(homeStore.runtime);
    }, { delay: 1000 }), []);

    return (
        <Button>每帧执行时间{runtime}ms</Button>
    );

}
